package com.naiterui.ehp.bs.pharmacist.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.esign.PreSignResultBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.pharmacist.exception.ExceptionCodes;
import com.naiterui.ehp.bs.pharmacist.feign.PharmacistEsignFeignClient;
import com.naiterui.ehp.bs.pharmacist.service.IRecommendService;
import com.naiterui.ehp.bs.pharmacist.vo.recommend.AuditParamVO;
import com.naiterui.ehp.bs.pharmacist.vo.recommend.MedicationRecomVO;
import com.naiterui.ehp.bs.pharmacist.vo.recommend.PrescriptionDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * @Description 处方接口
 * @Author gongbaoqiang
 * @Date 2019/12/3 10:49 AM
 */
@Slf4j
@Api(description = "处方接口", tags = SwaggerTag.PHARMACIST + SwaggerTag.RECOM)
@RequestMapping("pharmacist/recommend")
@RestController
public class PharmacistRecommendController {

    @Autowired
    private IRecommendService recommendService;
    @Autowired
    private PharmacistEsignFeignClient pharmacistEsignFeignClient;

    @ApiOperation("处方列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "药师id", name = "pharmacistId", required = true, dataType = "long"),
            @ApiImplicitParam(value = "审核状态(0:待审核,1:已通过 2.未通过,99:全部,100:我审核的(通过+不通过的))", name = "status", required = true, dataType = "int")
    })
    @PostMapping("/list")
    public ResponseVO<PageVO<MedicationRecomVO>> list(Long pharmacistId, Integer status, PageParamVO pageParamVO) throws BusinessException {
        log.info("获取处方列表：status={}", status);
        return PublicService.returnResponseVO(recommendService.findAll(pharmacistId, status, pageParamVO));
    }

    @ApiOperation("处方详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "处方id", name = "recomId", required = true, dataType = "long")
    })
    @PostMapping("/detail")
    public ResponseVO<PrescriptionDetailVO> detail(@NotNull Long recomId) throws BusinessException {
        log.info("获取处方详情：recomId={}", recomId);
        return PublicService.returnResponseVO(recommendService.findDetailById(recomId));
    }

    @ApiOperation("处方单药师签名预签名")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "处方id", name = "recomId", required = true, dataType = "long"),
            @ApiImplicitParam(value = "药师id", name = "pharmacistId", required = true, dataType = "long")
    })
    @PostMapping("/sign/pre")
    public ResponseVO<PreSignResultBO> preSign(Long recomId, Long pharmacistId) throws BusinessException {
        log.info("获取签名unionId：recomId={}", recomId);
        return PublicService.returnResponseVO(recommendService.getUnionId(recomId, pharmacistId));
    }

    @ApiOperation(value = "处方单药师签名", notes = "处方单药师签名", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "pharmacistId", value = "药师ID", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "recomId", value = "处方ID", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "uniqueId", value = "预签名id", dataType = "string", paramType = "query"),})
    @RequestMapping("sign")
    @ResponseBody
    public ResponseVO<Void> sign(@NotNull Long pharmacistId, @NotNull Long recomId, @NotNull String uniqueId, String pin) throws BusinessException {
        log.info("[recom]处方单药师签名. pharmacistId:{}, recomId:{}, uniqueId:{}, pin {}", pharmacistId, recomId, uniqueId, pin);
        recommendService.sign(pharmacistId, recomId, uniqueId, pin);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }


    @ApiOperation("处方药师审核")
    @PostMapping("/audit")
    public ResponseVO<Void> audit(AuditParamVO paramVO, HttpServletRequest request) throws BusinessException {
        Long pharmacistId = Long.parseLong(request.getParameter("pharmacistId"));
        log.info("获取签名unionId：paramVO={}", paramVO.toString());

        recommendService.audit(paramVO, pharmacistId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

}
